package com.example.munidingwei.ui.components

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp

@Composable
fun SimulationDialog(
    onDismiss: () -> Unit,
    onSimulate: (latitude: Double, longitude: Double) -> Unit,
    modifier: Modifier = Modifier
) {
    var latitude by remember { mutableStateOf("") }
    var longitude by remember { mutableStateOf("") }
    var latitudeError by remember { mutableStateOf("") }
    var longitudeError by remember { mutableStateOf("") }
    
    fun validateInputs(): Boolean {
        var isValid = true
        
        // 验证纬度
        latitudeError = ""
        try {
            val lat = latitude.toDouble()
            if (lat < -90 || lat > 90) {
                latitudeError = "纬度必须在-90到90之间"
                isValid = false
            }
        } catch (e: NumberFormatException) {
            latitudeError = "请输入有效的纬度"
            isValid = false
        }
        
        // 验证经度
        longitudeError = ""
        try {
            val lng = longitude.toDouble()
            if (lng < -180 || lng > 180) {
                longitudeError = "经度必须在-180到180之间"
                isValid = false
            }
        } catch (e: NumberFormatException) {
            longitudeError = "请输入有效的经度"
            isValid = false
        }
        
        return isValid
    }
    
    AlertDialog(
        onDismissRequest = onDismiss,
        title = {
            Text(
                text = "模拟位置",
                fontWeight = FontWeight.Bold
            )
        },
        text = {
            Column(
                verticalArrangement = Arrangement.spacedBy(16.dp)
            ) {
                Text(
                    text = "请输入要模拟的经纬度坐标：",
                    style = MaterialTheme.typography.bodyMedium
                )
                
                OutlinedTextField(
                    value = latitude,
                    onValueChange = { 
                        latitude = it
                        latitudeError = ""
                    },
                    label = { Text("纬度 (Latitude)") },
                    placeholder = { Text("例如: 39.9042") },
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
                    isError = latitudeError.isNotEmpty(),
                    modifier = Modifier.fillMaxWidth(),
                    supportingText = if (latitudeError.isNotEmpty()) {
                        { Text(latitudeError, color = MaterialTheme.colorScheme.error) }
                    } else null
                )
                
                OutlinedTextField(
                    value = longitude,
                    onValueChange = { 
                        longitude = it
                        longitudeError = ""
                    },
                    label = { Text("经度 (Longitude)") },
                    placeholder = { Text("例如: 116.4074") },
                    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
                    isError = longitudeError.isNotEmpty(),
                    modifier = Modifier.fillMaxWidth(),
                    supportingText = if (longitudeError.isNotEmpty()) {
                        { Text(longitudeError, color = MaterialTheme.colorScheme.error) }
                    } else null
                )
                
                Text(
                    text = "提示：北京天安门坐标约为 39.9042, 116.4074",
                    style = MaterialTheme.typography.bodySmall,
                    color = MaterialTheme.colorScheme.onSurfaceVariant
                )
            }
        },
        confirmButton = {
            Button(
                onClick = {
                    if (validateInputs()) {
                        onSimulate(latitude.toDouble(), longitude.toDouble())
                        onDismiss()
                    }
                },
                enabled = latitude.isNotEmpty() && longitude.isNotEmpty()
            ) {
                Text("开始模拟")
            }
        },
        dismissButton = {
            TextButton(onClick = onDismiss) {
                Text("取消")
            }
        }
    )
}
